﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bamboo.Utils;
using System.IO;

namespace Bamboo.ExeShell
{
    public enum PscpProtocol
    {
        AUTO = 0,
        SFTP = 1,
        SCP = 2
    }

    public class Pscp: PuttyBase
    {
        /// <summary>
        /// 是否启用安静模式
        /// 默认为true
        /// </summary>
        public bool Quiet { get; set; } = true;

        /// <summary>
        /// EXE文件名，默认是pscp
        /// </summary>
        public string PscpFilename { get; set; } = "pscp.exe";

        /// <summary>
        /// 传输协议
        /// 默认为SFTP
        /// </summary>
        public PscpProtocol Protocol { get; set; } = PscpProtocol.SFTP;

        private string GetCommonCommandText()
        {
            string protocolText;
            if (Protocol == PscpProtocol.SCP)
            {
                protocolText = "-scp";
            }
            else if (Protocol == PscpProtocol.SFTP)
            {
                protocolText = "-sftp";
            }
            else
            {
                protocolText = "";
            }
            string qText = Quiet ? "-q" : "";
            string ret = "";
            if (SSHAuthenticationType == PuttyBase.SSHAuthenticationEnum.密码)
            {
                string passwd = $"\"{Password.Replace("\"", "\"\"")}\"";
                ret = $"-batch {protocolText} {qText} -P {Port} -pw {passwd}";
            }
            else
            {
                 ret = $"-batch {protocolText} {qText} -P {Port} -i {PrivateKeyPath}";
            }
            return ret;
        }
        public void DownloadFile(string remoteFile, string localDir)
        {
            if (!Directory.Exists(localDir))
                Directory.CreateDirectory(localDir);

            Cmd.MaskWords.Add($"-pw {Password}");
            CheckAndRun(Cmd, PscpFilename, $"{GetCommonCommandText()} {Username}@{Hostname}:\"{remoteFile}\" \"{localDir}\"");
        }

        public void DownloadDir(string remoteDir, string localDir)
        {
            if (!Directory.Exists(localDir))
                Directory.CreateDirectory(localDir);

            Cmd.MaskWords.Add($"-pw {Password}");
            CheckAndRun(Cmd, PscpFilename, $"{GetCommonCommandText()} -r {Username}@{Hostname}:\"{remoteDir}\" \"{localDir}\"");
        }

        public void UploadFile(string localFile, string remoteDir)
        {
            if (!File.Exists(localFile))
                throw new Exception("本地文件不存在: " + localFile);

            Cmd.MaskWords.Add($"-pw {Password}");
            CheckAndRun(Cmd, PscpFilename, $"{GetCommonCommandText()} \"{localFile}\" {Username}@{Hostname}:\"{remoteDir}\"");
        }

        public void UploadDir(string localDir, string remoteDir)
        {
            if (!Directory.Exists(localDir))
                throw new Exception("本地目录不存在: " + localDir);

            Cmd.MaskWords.Add($"-pw {Password}");
            CheckAndRun(Cmd, PscpFilename, $"{GetCommonCommandText()} -r \"{localDir}\" {Username}@{Hostname}:\"{remoteDir}\"");
        }
    }
}
